home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / game / shoot / ADoomPPC_src.lha / ADoomPPC_src / g_game.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-18  |  41.4 KB  |  1,700 lines

  1. // Emacs style mode select   -*- C++ -*- 
  2. //-----------------------------------------------------------------------------
  3. //
  4. // $Id:$
  5. //
  6. // Copyright (C) 1993-1996 by id Software, Inc.
  7. //
  8. // This source is available for distribution and/or modification
  9. // only under the terms of the DOOM Source Code License as
  10. // published by id Software. All rights reserved.
  11. //
  12. // The source is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // FITNESS FOR A PARTICULAR PURPOSE. See the DOOM Source Code License
  15. // for more details.
  16. //
  17. // $Log:$
  18. //
  19. // DESCRIPTION:  none
  20. //
  21. //-----------------------------------------------------------------------------
  22.  
  23.  
  24. static const char
  25. rcsid[] = "$Id: g_game.c,v 1.8 1997/02/03 22:45:09 b1 Exp $";
  26.  
  27. #include <string.h>
  28. #include <stdlib.h>
  29.  
  30. #include "doomdef.h" 
  31. #include "doomstat.h"
  32.  
  33. #include "z_zone.h"
  34. #include "f_finale.h"
  35. #include "m_argv.h"
  36. #include "m_misc.h"
  37. #include "m_menu.h"
  38. #include "m_random.h"
  39. #include "i_system.h"
  40.  
  41. #include "p_setup.h"
  42. #include "p_saveg.h"
  43. #include "p_tick.h"
  44.  
  45. #include "d_main.h"
  46.  
  47. #include "wi_stuff.h"
  48. #include "hu_stuff.h"
  49. #include "st_stuff.h"
  50. #include "am_map.h"
  51.  
  52. // Needs access to LFB.
  53. #include "v_video.h"
  54.  
  55. #include "w_wad.h"
  56.  
  57. #include "p_local.h" 
  58.  
  59. #include "s_sound.h"
  60.  
  61. // Data.
  62. #include "dstrings.h"
  63. #include "sounds.h"
  64.  
  65. // SKY handling - still the wrong place.
  66. #include "r_data.h"
  67. #include "r_sky.h"
  68.  
  69.  
  70.  
  71. #include "g_game.h"
  72.  
  73.  
  74. //#define SAVEGAMESIZE  0x2c000
  75. #define SAVEGAMESIZE    0x50000
  76. #define SAVESTRINGSIZE  24
  77.  
  78.  
  79.  
  80. boolean G_CheckDemoStatus (void); 
  81. void    G_ReadDemoTiccmd (ticcmd_t* cmd); 
  82. void    G_WriteDemoTiccmd (ticcmd_t* cmd); 
  83. void    G_PlayerReborn (int player); 
  84. void    G_InitNew (skill_t skill, int episode, int map); 
  85.  
  86. void    G_DoReborn (int playernum); 
  87.  
  88. void    G_DoLoadLevel (void); 
  89. void    G_DoNewGame (void); 
  90. void    G_DoLoadGame (void); 
  91. void    G_DoPlayDemo (void); 
  92. void    G_DoCompleted (void); 
  93. void    G_DoVictory (void); 
  94. void    G_DoWorldDone (void); 
  95. void    G_DoSaveGame (void); 
  96.  
  97.  
  98. gameaction_t    gameaction; 
  99. gamestate_t     gamestate; 
  100. skill_t         gameskill; 
  101. boolean         respawnmonsters;
  102. int             gameepisode; 
  103. int             gamemap; 
  104.  
  105. boolean         paused; 
  106. boolean         sendpause;              // send a pause event next tic 
  107. boolean         sendsave;               // send a save event next tic 
  108. boolean         usergame;               // ok to save / end game 
  109.  
  110. boolean         timingdemo;             // if true, exit with report on completion 
  111. boolean         nodrawers;              // for comparative timing purposes 
  112. boolean         noblit;                 // for comparative timing purposes 
  113. int             starttime;              // for comparative timing purposes       
  114.  
  115. boolean         viewactive; 
  116.  
  117. boolean         deathmatch;             // only if started as net death 
  118. boolean         netgame;                // only true if packets are broadcast 
  119. boolean         playeringame[MAXPLAYERS]; 
  120. player_t        players[MAXPLAYERS]; 
  121.  
  122. int             consoleplayer;          // player taking events and displaying 
  123. int             displayplayer;          // view being displayed 
  124. int             gametic; 
  125. int             levelstarttic;          // gametic at level start 
  126. int             totalkills, totalitems, totalsecret;    // for intermission 
  127.  
  128. char            demoname[32]; 
  129. boolean         demorecording; 
  130. boolean         demoplayback; 
  131. boolean         netdemo; 
  132. byte*           demobuffer;
  133. byte*           demo_p;
  134. byte*           demoend; 
  135. boolean         singledemo;             // quit after playing a demo from cmdline 
  136.  
  137. boolean         precache = true;        // if true, load all graphics at start 
  138.  
  139. wbstartstruct_t wminfo;                 // parms for world map / intermission 
  140.  
  141. short           consistancy[MAXPLAYERS][BACKUPTICS]; 
  142.  
  143. byte*           savebuffer;
  144.  
  145. // Dehacked
  146. int initial_health=100;
  147. int initial_bullets=50;
  148.  
  149. // 
  150. // controls (have defaults) 
  151. // 
  152. int             key_right;
  153. int             key_left;
  154.  
  155. int             key_up;
  156. int             key_down; 
  157. int             key_strafeleft;
  158. int             key_straferight; 
  159. int             key_fire;
  160. int             key_use;
  161. int             key_strafe;
  162. int             key_speed; 
  163.  
  164. int             mousebfire; 
  165. int             mousebstrafe; 
  166. int             mousebforward; 
  167.  
  168. int             joybfire; 
  169. int             joybstrafe; 
  170. int             joybuse; 
  171. int             joybspeed; 
  172.  
  173.  
  174.  
  175. #define MAXPLMOVE               (forwardmove[1]) 
  176.  
  177. #define TURBOTHRESHOLD  0x32
  178.  
  179. fixed_t         forwardmove[2] = {0x19, 0x32}; 
  180. fixed_t         sidemove[2] = {0x18, 0x28}; 
  181. fixed_t         angleturn[3] = {640, 1280, 320};        // + slow turn 
  182.  
  183. #define SLOWTURNTICS    6 
  184.  
  185. #define NUMKEYS         256 
  186.  
  187. boolean         gamekeydown[NUMKEYS]; 
  188. int             turnheld;                               // for accelerative turning 
  189.  
  190. boolean         mousearray[4]; 
  191. boolean*        mousebuttons = &mousearray[1];          // allow [-1]
  192.  
  193. // mouse values are used once 
  194. int             mousex;
  195. int             mousey;         
  196.  
  197. int             dclicktime;
  198. int             dclickstate;
  199. int             dclicks; 
  200. int             dclicktime2;
  201. int             dclickstate2;
  202. int             dclicks2;
  203.  
  204. // joystick values are repeated 
  205. int             joyxmove;
  206. int             joyymove;
  207. boolean         joyarray[5]; 
  208. boolean*        joybuttons = &joyarray[1];              // allow [-1] 
  209.  
  210. int             savegameslot; 
  211. char            savedescription[32]; 
  212.  
  213.  
  214. #define BODYQUESIZE     32
  215.  
  216. mobj_t*         bodyque[BODYQUESIZE]; 
  217. int             bodyqueslot; 
  218.  
  219. void*           statcopy;                               // for statistics driver
  220.  
  221.  
  222.  
  223. int G_CmdChecksum (ticcmd_t* cmd) 
  224.     int         i;
  225.     int         sum = 0; 
  226.          
  227.     for (i=0 ; i< sizeof(*cmd)/4 - 1 ; i++) 
  228.         sum += ((int *)cmd)[i]; 
  229.                  
  230.     return sum; 
  231.  
  232.  
  233. //
  234. // G_BuildTiccmd
  235. // Builds a ticcmd from all of the available inputs
  236. // or reads it from the demo buffer. 
  237. // If recording a demo, write it out 
  238. // 
  239. void G_BuildTiccmd (ticcmd_t* cmd) 
  240.     int         i; 
  241.     boolean     strafe;
  242.     boolean     bstrafe; 
  243.     int         speed;
  244.     int         tspeed; 
  245.     int         forward;
  246.     int         side;
  247.     
  248.     ticcmd_t*   base;
  249.  
  250.     base = I_BaseTiccmd ();             // empty, or external driver
  251.     memcpy (cmd,base,sizeof(*cmd)); 
  252.         
  253.     cmd->consistancy = 
  254.         consistancy[consoleplayer][maketic%BACKUPTICS]; 
  255.  
  256.  
  257.     strafe = gamekeydown[key_strafe] || mousebuttons[mousebstrafe] 
  258.         || joybuttons[joybstrafe]; 
  259.     speed = gamekeydown[key_speed] || joybuttons[joybspeed];
  260.  
  261.     forward = side = 0;
  262.     
  263.     // use two stage accelerative turning
  264.     // on the keyboard and joystick
  265.     if (joyxmove < 0
  266.         || joyxmove > 0  
  267.         || gamekeydown[key_right]
  268.         || gamekeydown[key_left]) 
  269.         turnheld += ticdup; 
  270.     else 
  271.         turnheld = 0; 
  272.  
  273.     if (turnheld < SLOWTURNTICS) 
  274.         tspeed = 2;             // slow turn 
  275.     else 
  276.         tspeed = speed;
  277.     
  278.     // let movement keys cancel each other out
  279.     if (strafe) 
  280.     { 
  281.         if (gamekeydown[key_right]) 
  282.         {
  283.             // fprintf(stderr, "strafe right\n");
  284.             side += sidemove[speed]; 
  285.         }
  286.         if (gamekeydown[key_left]) 
  287.         {
  288.             //  fprintf(stderr, "strafe left\n");
  289.             side -= sidemove[speed]; 
  290.         }
  291.         if (joyxmove > 0) 
  292.             side += sidemove[speed]; 
  293.         if (joyxmove < 0) 
  294.             side -= sidemove[speed]; 
  295.  
  296.     } 
  297.     else 
  298.     { 
  299.         if (gamekeydown[key_right]) 
  300.             cmd->angleturn -= angleturn[tspeed]; 
  301.         if (gamekeydown[key_left]) 
  302.             cmd->angleturn += angleturn[tspeed]; 
  303.         if (joyxmove > 0) 
  304.             cmd->angleturn -= angleturn[tspeed]; 
  305.         if (joyxmove < 0) 
  306.             cmd->angleturn += angleturn[tspeed]; 
  307.     } 
  308.  
  309.     if (gamekeydown[key_up]) 
  310.     {
  311. //        fprintf(stderr, "up\n");
  312.         forward += forwardmove[speed];
  313. //                fprintf(stderr, "speed: %ld\n", forward); 
  314.     }
  315.     if (gamekeydown[key_down]) 
  316.     {
  317. //         fprintf(stderr, "down\n");
  318.         forward -= forwardmove[speed];
  319. //                fprintf(stderr, "speed: %ld\n", forward);
  320.     }
  321.     if (joyymove < 0) 
  322.         forward += forwardmove[speed]; 
  323.     if (joyymove > 0) 
  324.         forward -= forwardmove[speed]; 
  325.     if (gamekeydown[key_straferight]) 
  326.         side += sidemove[speed]; 
  327.     if (gamekeydown[key_strafeleft]) 
  328.         side -= sidemove[speed];
  329.     
  330.     // buttons
  331.     cmd->chatchar = HU_dequeueChatChar(); 
  332.  
  333.     if (gamekeydown[key_fire] || mousebuttons[mousebfire] 
  334.         || joybuttons[joybfire]) 
  335.         cmd->buttons |= BT_ATTACK; 
  336.  
  337.     if (gamekeydown[key_use] || joybuttons[joybuse] ) 
  338.     { 
  339.         cmd->buttons |= BT_USE;
  340.         // clear double clicks if hit use button 
  341.         dclicks = 0;                   
  342.     } 
  343.  
  344.     // chainsaw overrides 
  345.     for (i=0 ; i<NUMWEAPONS-1 ; i++)        
  346.         if (gamekeydown['1'+i]) 
  347.         { 
  348.             cmd->buttons |= BT_CHANGE; 
  349.             cmd->buttons |= i<<BT_WEAPONSHIFT; 
  350.             break; 
  351.         }
  352.     
  353.     // mouse
  354.     if (mousebuttons[mousebforward]) 
  355.         forward += forwardmove[speed];
  356.     
  357.     // forward double click
  358.     if (mousebuttons[mousebforward] != dclickstate && dclicktime > 1 ) 
  359.     { 
  360.         dclickstate = mousebuttons[mousebforward]; 
  361.         if (dclickstate) 
  362.             dclicks++; 
  363.         if (dclicks == 2) 
  364.         { 
  365.             cmd->buttons |= BT_USE; 
  366.             dclicks = 0; 
  367.         } 
  368.         else 
  369.             dclicktime = 0; 
  370.     } 
  371.     else 
  372.     { 
  373.         dclicktime += ticdup; 
  374.         if (dclicktime > 20) 
  375.         { 
  376.             dclicks = 0; 
  377.             dclickstate = 0; 
  378.         } 
  379.     }
  380.     
  381.     // strafe double click
  382.     bstrafe =
  383.         mousebuttons[mousebstrafe] 
  384.         || joybuttons[joybstrafe]; 
  385.     if (bstrafe != dclickstate2 && dclicktime2 > 1 ) 
  386.     { 
  387.         dclickstate2 = bstrafe; 
  388.         if (dclickstate2) 
  389.             dclicks2++; 
  390.         if (dclicks2 == 2) 
  391.         { 
  392.             cmd->buttons |= BT_USE; 
  393.             dclicks2 = 0; 
  394.         } 
  395.         else 
  396.             dclicktime2 = 0; 
  397.     } 
  398.     else 
  399.     { 
  400.         dclicktime2 += ticdup; 
  401.         if (dclicktime2 > 20) 
  402.         { 
  403.             dclicks2 = 0; 
  404.             dclickstate2 = 0; 
  405.         } 
  406.     } 
  407.  
  408.     forward += mousey; 
  409.     if (strafe) 
  410.         side += mousex*2; 
  411.     else 
  412.         cmd->angleturn -= mousex*0x8; 
  413.  
  414.     mousex = mousey = 0; 
  415.          
  416.     if (forward > MAXPLMOVE) 
  417.         forward = MAXPLMOVE; 
  418.     else if (forward < -MAXPLMOVE) 
  419.         forward = -MAXPLMOVE; 
  420.     if (side > MAXPLMOVE) 
  421.         side = MAXPLMOVE; 
  422.     else if (side < -MAXPLMOVE) 
  423.         side = -MAXPLMOVE; 
  424.  
  425. //        fprintf(stderr, "forward: %ld\n", forward);
  426.     cmd->forwardmove += forward;
  427. //        fprintf(stderr, "cmd->forwardmove: %ld\n", cmd->forwardmove);
  428.     cmd->sidemove += side;
  429.     
  430.     // special buttons
  431.     if (sendpause) 
  432.     { 
  433.         sendpause = false; 
  434.         cmd->buttons = BT_SPECIAL | BTS_PAUSE; 
  435.     } 
  436.  
  437.     if (sendsave) 
  438.     { 
  439.         sendsave = false; 
  440.         cmd->buttons = BT_SPECIAL | BTS_SAVEGAME | (savegameslot<<BTS_SAVESHIFT); 
  441.     } 
  442.  
  443.  
  444. //
  445. // G_DoLoadLevel 
  446. //
  447. extern  gamestate_t     wipegamestate; 
  448.  
  449. void G_DoLoadLevel (void) 
  450.     int             i; 
  451.  
  452.     // Set the sky map.
  453.     // First thing, we have a dummy sky texture name,
  454.     //  a flat. The data is in the WAD only because
  455.     //  we look for an actual index, instead of simply
  456.     //  setting one.
  457.     skyflatnum = R_FlatNumForName ( SKYFLATNAME );
  458.  
  459.     // DOOM determines the sky texture to be used
  460.     // depending on the current episode, and the game version.
  461.     if ( (gamemode == commercial)
  462.          || ( gamemode == pack_tnt )
  463.          || ( gamemode == pack_plut ) )
  464.     {
  465.         skytexture = R_TextureNumForName ("SKY3");
  466.         if (gamemap < 12)
  467.             skytexture = R_TextureNumForName ("SKY1");
  468.         else
  469.             if (gamemap < 21)
  470.                 skytexture = R_TextureNumForName ("SKY2");
  471.     }
  472.  
  473.     levelstarttic = gametic;        // for time calculation
  474.     
  475.     if (wipegamestate == GS_LEVEL) 
  476.         wipegamestate = -1;             // force a wipe 
  477.  
  478.     gamestate = GS_LEVEL; 
  479.  
  480.     for (i=0 ; i<MAXPLAYERS ; i++) 
  481.     { 
  482.         if (playeringame[i] && players[i].playerstate == PST_DEAD) 
  483.             players[i].playerstate = PST_REBORN; 
  484.         memset (players[i].frags,0,sizeof(players[i].frags)); 
  485.     } 
  486.                  
  487.     P_SetupLevel (gameepisode, gamemap, 0, gameskill);    
  488.     displayplayer = consoleplayer;              // view the guy you are playing    
  489.     starttime = I_GetTime (); 
  490.     gameaction = ga_nothing; 
  491.     Z_CheckHeap ();
  492.     
  493.     // clear cmd building stuff
  494.     memset (gamekeydown, 0, sizeof(gamekeydown)); 
  495.     joyxmove = joyymove = 0; 
  496.     mousex = mousey = 0; 
  497.     sendpause = sendsave = paused = false; 
  498.     memset (mousebuttons, 0, sizeof(mousebuttons)); 
  499.     memset (joybuttons, 0, sizeof(joybuttons)); 
  500.  
  501.  
  502. //
  503. // G_Responder  
  504. // Get info needed to make ticcmd_ts for the players.
  505. // 
  506. boolean G_Responder (event_t* ev) 
  507.     // allow spy mode changes even during the demo
  508.     if (gamestate == GS_LEVEL && ev->type == ev_keydown 
  509.         && ev->data1 == KEY_F12 && (singledemo || !deathmatch) )
  510.     {
  511.         // spy mode 
  512.         do 
  513.         { 
  514.             displayplayer++; 
  515.             if (displayplayer == MAXPLAYERS) 
  516.                 displayplayer = 0; 
  517.         } while (!playeringame[displayplayer] && displayplayer != consoleplayer); 
  518.         return true; 
  519.     }
  520.     
  521.     // any other key pops up menu if in demos
  522.     if (gameaction == ga_nothing && !singledemo && 
  523.         (demoplayback || gamestate == GS_DEMOSCREEN) 
  524.         ) 
  525.     { 
  526.         if (ev->type == ev_keydown ||  
  527.             (ev->type == ev_mouse && ev->data1) || 
  528.             (ev->type == ev_joystick && ev->data1) ) 
  529.         { 
  530.             M_StartControlPanel (); 
  531.             return true; 
  532.         } 
  533.         return false; 
  534.     } 
  535.  
  536.     if (gamestate == GS_LEVEL) 
  537.     { 
  538. #if 0 
  539.         if (devparm && ev->type == ev_keydown && ev->data1 == ';') 
  540.         { 
  541.             G_DeathMatchSpawnPlayer (0); 
  542.             return true; 
  543.         } 
  544. #endif 
  545.         if (HU_Responder (ev)) 
  546.             return true;        // chat ate the event 
  547.         if (ST_Responder (ev)) 
  548.             return true;        // status window ate it 
  549.         if (AM_Responder (ev)) 
  550.             return true;        // automap ate it 
  551.     } 
  552.          
  553.     if (gamestate == GS_FINALE) 
  554.     { 
  555.         if (F_Responder (ev)) 
  556.             return true;        // finale ate the event 
  557.     } 
  558.          
  559.     switch (ev->type) 
  560.     { 
  561.       case ev_keydown: 
  562.         if (ev->data1 == KEY_PAUSE) 
  563.         { 
  564.             sendpause = true; 
  565.             return true; 
  566.         } 
  567.         if (ev->data1 <NUMKEYS) 
  568.             gamekeydown[ev->data1] = true; 
  569.         return true;    // eat key down events 
  570.  
  571.       case ev_keyup: 
  572.         if (ev->data1 <NUMKEYS) 
  573.             gamekeydown[ev->data1] = false; 
  574.         return false;   // always let key up events filter down 
  575.                  
  576.       case ev_mouse: 
  577.         mousebuttons[0] = ev->data1 & 1; 
  578.         mousebuttons[1] = ev->data1 & 2; 
  579.         mousebuttons[2] = ev->data1 & 4; 
  580.         mousex = ev->data2*(mouseSensitivity+5)/10; 
  581.         mousey = ev->data3*(mouseSensitivity+5)/10; 
  582.         return true;    // eat events 
  583.  
  584.       case ev_joystick: 
  585.         joybuttons[0] = ev->data1 & 1; 
  586.         joybuttons[1] = ev->data1 & 2; 
  587.         joybuttons[2] = ev->data1 & 4; 
  588.         joybuttons[3] = ev->data1 & 8; 
  589.         joyxmove = ev->data2; 
  590.         joyymove = ev->data3; 
  591.         return true;    // eat events 
  592.  
  593.       default: 
  594.         break; 
  595.     } 
  596.  
  597.     return false; 
  598.  
  599.  
  600.  
  601. //
  602. // G_Ticker
  603. // Make ticcmd_ts for the players.
  604. //
  605. void G_Ticker (void) 
  606.     int         i;
  607.     int         buf; 
  608.     ticcmd_t*   cmd;
  609.     
  610.     // do player reborns if needed
  611.     for (i=0 ; i<MAXPLAYERS ; i++) 
  612.         if (playeringame[i] && players[i].playerstate == PST_REBORN) 
  613.             G_DoReborn (i);
  614.     
  615.     // do things to change the game state
  616.     while (gameaction != ga_nothing) 
  617.     { 
  618.         switch (gameaction) 
  619.         { 
  620.           case ga_loadlevel: 
  621.             G_DoLoadLevel (); 
  622.             break; 
  623.           case ga_newgame: 
  624.             G_DoNewGame (); 
  625.             break; 
  626.           case ga_loadgame: 
  627.             G_DoLoadGame (); 
  628.             break; 
  629.           case ga_savegame: 
  630.             G_DoSaveGame (); 
  631.             break; 
  632.           case ga_playdemo: 
  633.             G_DoPlayDemo (); 
  634.             break; 
  635.           case ga_completed: 
  636.             G_DoCompleted (); 
  637.             break; 
  638.           case ga_victory: 
  639.             F_StartFinale (); 
  640.             break; 
  641.           case ga_worlddone: 
  642.             G_DoWorldDone (); 
  643.             break; 
  644.           case ga_screenshot: 
  645.             M_ScreenShot (); 
  646.             gameaction = ga_nothing; 
  647.             break; 
  648.           case ga_nothing: 
  649.             break; 
  650.         } 
  651.     }
  652.     
  653.     // get commands, check consistancy,
  654.     // and build new consistancy check
  655.     buf = (gametic/ticdup)%BACKUPTICS; 
  656.  
  657.     for (i=0 ; i<MAXPLAYERS ; i++)
  658.     {
  659.         if (playeringame[i]) 
  660.         { 
  661.             cmd = &players[i].cmd; 
  662.  
  663.             memcpy (cmd, &netcmds[i][buf], sizeof(ticcmd_t)); 
  664.  
  665.             if (demoplayback) 
  666.                 G_ReadDemoTiccmd (cmd); 
  667.             if (demorecording) 
  668.                 G_WriteDemoTiccmd (cmd);
  669.             
  670.             // check for turbo cheats
  671.             if (cmd->forwardmove > TURBOTHRESHOLD 
  672.                 && !(gametic&31) && ((gametic>>5)&3) == i )
  673.             {
  674.                 static char turbomessage[80];
  675.                 extern char *player_names[4];
  676.                 sprintf (turbomessage, "%s is turbo!",player_names[i]);
  677.                 players[consoleplayer].message = turbomessage;
  678.             }
  679.                         
  680.             if (netgame && !netdemo && !(gametic%ticdup) ) 
  681.             { 
  682.                 if (gametic > BACKUPTICS 
  683.                     && consistancy[i][buf] != cmd->consistancy) 
  684.                 { 
  685.                     I_Error ("consistency failure (%i should be %i)",
  686.                              cmd->consistancy, consistancy[i][buf]); 
  687.                 } 
  688.                 if (players[i].mo) 
  689.                     consistancy[i][buf] = players[i].mo->x; 
  690.                 else 
  691.                     consistancy[i][buf] = rndindex; 
  692.             } 
  693.         }
  694.     }
  695.     
  696.     // check for special buttons
  697.     for (i=0 ; i<MAXPLAYERS ; i++)
  698.     {
  699.         if (playeringame[i]) 
  700.         { 
  701.             if (players[i].cmd.buttons & BT_SPECIAL) 
  702.             { 
  703.                 switch (players[i].cmd.buttons & BT_SPECIALMASK) 
  704.                 { 
  705.                   case BTS_PAUSE: 
  706.                     paused ^= 1; 
  707.                     if (paused) 
  708.                         S_PauseSound (); 
  709.                     else 
  710.                         S_ResumeSound (); 
  711.                     break; 
  712.                                          
  713.                   case BTS_SAVEGAME: 
  714.                     if (!savedescription[0]) 
  715.                         strcpy (savedescription, "NET GAME"); 
  716.                     savegameslot =  
  717.                         (players[i].cmd.buttons & BTS_SAVEMASK)>>BTS_SAVESHIFT; 
  718.                     gameaction = ga_savegame; 
  719.                     break; 
  720.                 } 
  721.             } 
  722.         }
  723.     }
  724.     
  725.     // do main actions
  726.     switch (gamestate) 
  727.     { 
  728.       case GS_LEVEL: 
  729.         P_Ticker (); 
  730.         ST_Ticker (); 
  731.         AM_Ticker (); 
  732.         HU_Ticker ();            
  733.         break; 
  734.          
  735.       case GS_INTERMISSION: 
  736.         WI_Ticker (); 
  737.         break; 
  738.                          
  739.       case GS_FINALE: 
  740.         F_Ticker (); 
  741.         break; 
  742.  
  743.       case GS_DEMOSCREEN: 
  744.         D_PageTicker (); 
  745.         break; 
  746.     }        
  747.  
  748.  
  749. //
  750. // PLAYER STRUCTURE FUNCTIONS
  751. // also see P_SpawnPlayer in P_Things
  752. //
  753.  
  754. //
  755. // G_InitPlayer 
  756. // Called at the start.
  757. // Called by the game initialization functions.
  758. //
  759. void G_InitPlayer (int player) 
  760.     player_t*   p; 
  761.  
  762.     // set up the saved info         
  763.     p = &players[player]; 
  764.          
  765.     // clear everything else to defaults 
  766.     G_PlayerReborn (player); 
  767.          
  768.  
  769.  
  770.  
  771. //
  772. // G_PlayerFinishLevel
  773. // Can when a player completes a level.
  774. //
  775. void G_PlayerFinishLevel (int player) 
  776.     player_t*   p; 
  777.          
  778.     p = &players[player]; 
  779.          
  780.     memset (p->powers, 0, sizeof (p->powers)); 
  781.     memset (p->cards, 0, sizeof (p->cards)); 
  782.     p->mo->flags &= ~MF_SHADOW;         // cancel invisibility 
  783.     p->extralight = 0;                  // cancel gun flashes 
  784.     p->fixedcolormap = 0;               // cancel ir gogles 
  785.     p->damagecount = 0;                 // no palette changes 
  786.     p->bonuscount = 0; 
  787.  
  788.  
  789. //
  790. // G_PlayerReborn
  791. // Called after a player dies 
  792. // almost everything is cleared and initialized 
  793. //
  794. void G_PlayerReborn (int player) 
  795.     player_t*   p; 
  796.     int         i; 
  797.     int         frags[MAXPLAYERS]; 
  798.     int         killcount;
  799.     int         itemcount;
  800.     int         secretcount; 
  801.          
  802.     memcpy (frags,players[player].frags,sizeof(frags)); 
  803.     killcount = players[player].killcount; 
  804.     itemcount = players[player].itemcount; 
  805.     secretcount = players[player].secretcount; 
  806.          
  807.     p = &players[player]; 
  808.     memset (p, 0, sizeof(*p)); 
  809.  
  810.     memcpy (players[player].frags, frags, sizeof(players[player].frags)); 
  811.     players[player].killcount = killcount; 
  812.     players[player].itemcount = itemcount; 
  813.     players[player].secretcount = secretcount; 
  814.  
  815.     p->usedown = p->attackdown = true;  // don't do anything immediately 
  816.     p->playerstate = PST_LIVE;       
  817.     p->health = initial_health; // Dehacked
  818.     p->readyweapon = p->pendingweapon = wp_pistol; 
  819.     p->weaponowned[wp_fist] = true; 
  820.     p->weaponowned[wp_pistol] = true; 
  821.     p->ammo[am_clip] = initial_bullets; // Dehacked
  822.          
  823.     for (i=0 ; i<NUMAMMO ; i++) 
  824.         p->maxammo[i] = maxammo[i]; 
  825.                  
  826. }
  827.  
  828. //
  829. // G_CheckSpot  
  830. // Returns false if the player cannot be respawned
  831. // at the given mapthing_t spot  
  832. // because something is occupying it 
  833. //
  834. void P_SpawnPlayer (mapthing_t* mthing); 
  835.  
  836. boolean
  837. G_CheckSpot
  838. ( int           playernum,
  839.   mapthing_t*   mthing ) 
  840.     fixed_t             x;
  841.     fixed_t             y; 
  842.     subsector_t*        ss; 
  843.     unsigned            an; 
  844.     mobj_t*             mo; 
  845.     int                 i;
  846.         
  847.     if (!players[playernum].mo)
  848.     {
  849.         // first spawn of level, before corpses
  850.         for (i=0 ; i<playernum ; i++)
  851.             if (players[i].mo->x == mthing->x << FRACBITS
  852.                 && players[i].mo->y == mthing->y << FRACBITS)
  853.                 return false;   
  854.         return true;
  855.     }
  856.                 
  857.     x = mthing->x << FRACBITS; 
  858.     y = mthing->y << FRACBITS; 
  859.          
  860.     if (!P_CheckPosition (players[playernum].mo, x, y) ) 
  861.         return false; 
  862.  
  863.     // flush an old corpse if needed 
  864.     if (bodyqueslot >= BODYQUESIZE) 
  865.         P_RemoveMobj (bodyque[bodyqueslot%BODYQUESIZE]); 
  866.     bodyque[bodyqueslot%BODYQUESIZE] = players[playernum].mo; 
  867.     bodyqueslot++; 
  868.         
  869.     // spawn a teleport fog 
  870.     ss = R_PointInSubsector (x,y); 
  871.     an = ( ANG45 * (mthing->angle/45) ) >> ANGLETOFINESHIFT; 
  872.  
  873.     mo = P_SpawnMobj (x+20*finecosine[an], y+20*finesine[an] 
  874.                       , ss->sector->floorheight 
  875.                       , MT_TFOG); 
  876.          
  877.     if (players[consoleplayer].viewz != 1) 
  878.         S_StartSound (mo, sfx_telept);  // don't start sound on first frame 
  879.  
  880.     return true; 
  881.  
  882.  
  883. //
  884. // G_DeathMatchSpawnPlayer 
  885. // Spawns a player at one of the random death match spots 
  886. // called at level load and each death 
  887. //
  888. void G_DeathMatchSpawnPlayer (int playernum) 
  889.     int             i,j; 
  890.     int                         selections; 
  891.          
  892.     selections = deathmatch_p - deathmatchstarts; 
  893.     if (selections < 4) 
  894.         I_Error ("Only %i deathmatch spots, 4 required", selections); 
  895.  
  896.     for (j=0 ; j<20 ; j++) 
  897.     { 
  898.         i = P_Random() % selections; 
  899.         if (G_CheckSpot (playernum, &deathmatchstarts[i]) ) 
  900.         { 
  901.             deathmatchstarts[i].type = playernum+1; 
  902.             P_SpawnPlayer (&deathmatchstarts[i]); 
  903.             return; 
  904.         } 
  905.     } 
  906.  
  907.     // no good spot, so the player will probably get stuck 
  908.     P_SpawnPlayer (&playerstarts[playernum]); 
  909.  
  910. //
  911. // G_DoReborn 
  912. // 
  913. void G_DoReborn (int playernum) 
  914.     int                             i; 
  915.          
  916.     if (!netgame)
  917.     {
  918.         // reload the level from scratch
  919.         gameaction = ga_loadlevel;  
  920.     }
  921.     else 
  922.     {
  923.         // respawn at the start
  924.  
  925.         // first dissasociate the corpse 
  926.         players[playernum].mo->player = NULL;   
  927.                  
  928.         // spawn at random spot if in death match 
  929.         if (deathmatch) 
  930.         { 
  931.             G_DeathMatchSpawnPlayer (playernum); 
  932.             return; 
  933.         } 
  934.                  
  935.         if (G_CheckSpot (playernum, &playerstarts[playernum]) ) 
  936.         { 
  937.             P_SpawnPlayer (&playerstarts[playernum]); 
  938.             return; 
  939.         }
  940.         
  941.         // try to spawn at one of the other players spots 
  942.         for (i=0 ; i<MAXPLAYERS ; i++)
  943.         {
  944.             if (G_CheckSpot (playernum, &playerstarts[i]) ) 
  945.             { 
  946.                 playerstarts[i].type = playernum+1;     // fake as other player 
  947.                 P_SpawnPlayer (&playerstarts[i]); 
  948.                 playerstarts[i].type = i+1;             // restore 
  949.                 return; 
  950.             }       
  951.             // he's going to be inside something.  Too bad.
  952.         }
  953.         P_SpawnPlayer (&playerstarts[playernum]); 
  954.     } 
  955.  
  956.  
  957. void G_ScreenShot (void) 
  958.     gameaction = ga_screenshot; 
  959.  
  960.  
  961.  
  962. // DOOM Par Times
  963. int pars[4][10] = 
  964.     {0}, 
  965.     {0,30,75,120,90,165,180,180,30,165}, 
  966.     {0,90,90,90,120,90,360,240,30,170}, 
  967.     {0,90,45,90,150,90,90,165,30,135} 
  968. }; 
  969.  
  970. // DOOM II Par Times
  971. int cpars[32] =
  972. {
  973.     30,90,120,120,90,150,120,120,270,90,        //  1-10
  974.     210,150,150,150,210,150,420,150,210,150,    // 11-20
  975.     240,150,180,150,150,300,330,420,300,180,    // 21-30
  976.     120,30                                      // 31-32
  977. };
  978.  
  979.  
  980. //
  981. // G_DoCompleted 
  982. //
  983. boolean         secretexit; 
  984. extern char*    pagename; 
  985.  
  986. void G_ExitLevel (void) 
  987.     secretexit = false; 
  988.     gameaction = ga_completed; 
  989.  
  990. // Here's for the german edition.
  991. void G_SecretExitLevel (void) 
  992.     // IF NO WOLF3D LEVELS, NO SECRET EXIT!
  993.     if ( (gamemode == commercial)
  994.       && (W_CheckNumForName("map31")<0))
  995.         secretexit = false;
  996.     else
  997.         secretexit = true; 
  998.     gameaction = ga_completed; 
  999.  
  1000. void G_DoCompleted (void) 
  1001.     int             i; 
  1002.          
  1003.     gameaction = ga_nothing; 
  1004.  
  1005.     for (i=0 ; i<MAXPLAYERS ; i++) 
  1006.         if (playeringame[i]) 
  1007.             G_PlayerFinishLevel (i);        // take away cards and stuff 
  1008.          
  1009.     if (automapactive) 
  1010.         AM_Stop (); 
  1011.         
  1012.     if ( gamemode != commercial)
  1013.         switch(gamemap)
  1014.         {
  1015.           case 8:
  1016.             gameaction = ga_victory;
  1017.             return;
  1018.           case 9: 
  1019.             for (i=0 ; i<MAXPLAYERS ; i++) 
  1020.                 players[i].didsecret = true; 
  1021.             break;
  1022.         }
  1023.                 
  1024. //#if 0  Hmmm - why?
  1025.     if ( (gamemap == 8)
  1026.          && (gamemode != commercial) ) 
  1027.     {
  1028.         // victory 
  1029.         gameaction = ga_victory; 
  1030.         return; 
  1031.     } 
  1032.          
  1033.     if ( (gamemap == 9)
  1034.          && (gamemode != commercial) ) 
  1035.     {
  1036.         // exit secret level 
  1037.         for (i=0 ; i<MAXPLAYERS ; i++) 
  1038.             players[i].didsecret = true; 
  1039.     } 
  1040. //#endif
  1041.     
  1042.          
  1043.     wminfo.didsecret = players[consoleplayer].didsecret; 
  1044.     wminfo.epsd = gameepisode -1; 
  1045.     wminfo.last = gamemap -1;
  1046.     
  1047.     // wminfo.next is 0 biased, unlike gamemap
  1048.     if ( gamemode == commercial)
  1049.     {
  1050.         if (secretexit)
  1051.             switch(gamemap)
  1052.             {
  1053.               case 15: wminfo.next = 30; break;
  1054.               case 31: wminfo.next = 31; break;
  1055.             }
  1056.         else
  1057.             switch(gamemap)
  1058.             {
  1059.               case 31:
  1060.               case 32: wminfo.next = 15; break;
  1061.               default: wminfo.next = gamemap;
  1062.             }
  1063.     }
  1064.     else
  1065.     {
  1066.         if (secretexit) 
  1067.             wminfo.next = 8;    // go to secret level 
  1068.         else if (gamemap == 9) 
  1069.         {
  1070.             // returning from secret level 
  1071.             switch (gameepisode) 
  1072.             { 
  1073.               case 1: 
  1074.                 wminfo.next = 3; 
  1075.                 break; 
  1076.               case 2: 
  1077.                 wminfo.next = 5; 
  1078.                 break; 
  1079.               case 3: 
  1080.                 wminfo.next = 6; 
  1081.                 break; 
  1082.               case 4:
  1083.                 wminfo.next = 2;
  1084.                 break;
  1085.             }                
  1086.         } 
  1087.         else 
  1088.             wminfo.next = gamemap;          // go to next level 
  1089.     }
  1090.                  
  1091.     wminfo.maxkills = totalkills; 
  1092.     wminfo.maxitems = totalitems; 
  1093.     wminfo.maxsecret = totalsecret; 
  1094.     wminfo.maxfrags = 0; 
  1095.     if ( gamemode == commercial )
  1096.         wminfo.partime = 35*cpars[gamemap-1]; 
  1097.     else
  1098.         wminfo.partime = 35*pars[gameepisode][gamemap]; 
  1099.     wminfo.pnum = consoleplayer; 
  1100.  
  1101.     for (i=0 ; i<MAXPLAYERS ; i++) 
  1102.     { 
  1103.         wminfo.plyr[i].in = playeringame[i]; 
  1104.         wminfo.plyr[i].skills = players[i].killcount; 
  1105.         wminfo.plyr[i].sitems = players[i].itemcount; 
  1106.         wminfo.plyr[i].ssecret = players[i].secretcount; 
  1107.         wminfo.plyr[i].stime = leveltime; 
  1108.         memcpy (wminfo.plyr[i].frags, players[i].frags 
  1109.                 , sizeof(wminfo.plyr[i].frags)); 
  1110.     } 
  1111.  
  1112.     gamestate = GS_INTERMISSION; 
  1113.     viewactive = false; 
  1114.     automapactive = false; 
  1115.  
  1116.     if (statcopy)
  1117.         memcpy (statcopy, &wminfo, sizeof(wminfo));
  1118.         
  1119.     WI_Start (&wminfo); 
  1120.  
  1121.  
  1122. //
  1123. // G_WorldDone 
  1124. //
  1125. void G_WorldDone (void) 
  1126.     gameaction = ga_worlddone; 
  1127.  
  1128.     if (secretexit) 
  1129.         players[consoleplayer].didsecret = true; 
  1130.  
  1131.     if ( gamemode == commercial )
  1132.     {
  1133.         switch (gamemap)
  1134.         {
  1135.           case 15:
  1136.           case 31:
  1137.             if (!secretexit)
  1138.                 break;
  1139.           case 6:
  1140.           case 11:
  1141.           case 20:
  1142.           case 30:
  1143.             F_StartFinale ();
  1144.             break;
  1145.         }
  1146.     }
  1147.  
  1148. void G_DoWorldDone (void) 
  1149. {        
  1150.     gamestate = GS_LEVEL; 
  1151.     gamemap = wminfo.next+1; 
  1152.     G_DoLoadLevel (); 
  1153.     gameaction = ga_nothing; 
  1154.     viewactive = true; 
  1155.  
  1156.  
  1157.  
  1158. //
  1159. // G_InitFromSavegame
  1160. // Can be called by the startup code or the menu task. 
  1161. //
  1162. extern boolean setsizeneeded;
  1163. void R_ExecuteSetViewSize (void);
  1164.  
  1165. char    savename[256];
  1166.  
  1167. void G_LoadGame (char* name) 
  1168.     strcpy (savename, name); 
  1169.     gameaction = ga_loadgame; 
  1170.  
  1171. #define VERSIONSIZE             16 
  1172.  
  1173.  
  1174. void G_DoLoadGame (void) 
  1175.     int         length; 
  1176.     int         i; 
  1177.     int         a,b,c; 
  1178.     char        vcheck[VERSIONSIZE]; 
  1179.          
  1180.     gameaction = ga_nothing; 
  1181.          
  1182.     length = M_ReadFile (savename, &savebuffer); 
  1183.     save_p = savebuffer + SAVESTRINGSIZE;
  1184.     
  1185.     // skip the description field 
  1186.     memset (vcheck,0,sizeof(vcheck)); 
  1187.     sprintf (vcheck,"version %i",VERSION); 
  1188.     if (strcmp (save_p, vcheck)) 
  1189.         return;                         // bad version 
  1190.     save_p += VERSIONSIZE; 
  1191.                          
  1192.     gameskill = *save_p++; 
  1193.     gameepisode = *save_p++; 
  1194.     gamemap = *save_p++; 
  1195.     for (i=0 ; i<MAXPLAYERS ; i++) 
  1196.         playeringame[i] = *save_p++; 
  1197.  
  1198.     // load a base level 
  1199.     G_InitNew (gameskill, gameepisode, gamemap); 
  1200.  
  1201.     // get the times 
  1202.     a = *save_p++; 
  1203.     b = *save_p++; 
  1204.     c = *save_p++; 
  1205.     leveltime = (a<<16) + (b<<8) + c; 
  1206.          
  1207.     // dearchive all the modifications
  1208.     P_UnArchivePlayers (); 
  1209.     P_UnArchiveWorld (); 
  1210.     P_UnArchiveThinkers (); 
  1211.     P_UnArchiveSpecials (); 
  1212.  
  1213.     if (*save_p != 0x1d) 
  1214.         I_Error ("Bad savegame");
  1215.     
  1216.     // done 
  1217.     Z_Free (savebuffer); 
  1218.  
  1219.     if (setsizeneeded)
  1220.         R_ExecuteSetViewSize ();
  1221.     
  1222.     // draw the pattern into the back screen
  1223.     R_FillBackScreen ();   
  1224.  
  1225.  
  1226. //
  1227. // G_SaveGame
  1228. // Called by the menu task.
  1229. // Description is a 24 byte text string 
  1230. //
  1231. void
  1232. G_SaveGame
  1233. ( int   slot,
  1234.   char* description ) 
  1235.     savegameslot = slot; 
  1236.     strcpy (savedescription, description); 
  1237.     sendsave = true; 
  1238.  
  1239. void G_DoSaveGame (void) 
  1240.     char        name[100]; 
  1241.     char        name2[VERSIONSIZE]; 
  1242.     char*       description; 
  1243.     int         length; 
  1244.     int         i; 
  1245.         
  1246.     if (M_CheckParm("-cdrom"))
  1247.         sprintf(name,"c:\\doomdata\\"SAVEGAMENAME"%d.dsg",savegameslot);
  1248.     else
  1249.         sprintf (name,SAVEGAMENAME"%d.dsg",savegameslot); 
  1250.     description = savedescription; 
  1251.          
  1252.     save_p = savebuffer = screens[1]+0x4000; 
  1253.          
  1254.     memcpy (save_p, description, SAVESTRINGSIZE); 
  1255.     save_p += SAVESTRINGSIZE; 
  1256.     memset (name2,0,sizeof(name2)); 
  1257.     sprintf (name2,"version %i",VERSION); 
  1258.     memcpy (save_p, name2, VERSIONSIZE); 
  1259.     save_p += VERSIONSIZE; 
  1260.          
  1261.     *save_p++ = gameskill; 
  1262.     *save_p++ = gameepisode; 
  1263.     *save_p++ = gamemap; 
  1264.     for (i=0 ; i<MAXPLAYERS ; i++) 
  1265.         *save_p++ = playeringame[i]; 
  1266.     *save_p++ = leveltime>>16; 
  1267.     *save_p++ = leveltime>>8; 
  1268.     *save_p++ = leveltime; 
  1269.  
  1270.     P_ArchivePlayers (); 
  1271.     P_ArchiveWorld (); 
  1272.     P_ArchiveThinkers (); 
  1273.     P_ArchiveSpecials (); 
  1274.          
  1275.     *save_p++ = 0x1d;           // consistancy marker 
  1276.          
  1277.     length = save_p - savebuffer; 
  1278.     if (length > SAVEGAMESIZE) 
  1279.         I_Error ("Savegame buffer overrun"); 
  1280.     M_WriteFile (name, savebuffer, length); 
  1281.     gameaction = ga_nothing; 
  1282.     savedescription[0] = 0;              
  1283.          
  1284.     players[consoleplayer].message = GGSAVED; 
  1285.  
  1286.     // draw the pattern into the back screen
  1287.     R_FillBackScreen ();        
  1288.  
  1289.  
  1290. //
  1291. // G_InitNew
  1292. // Can be called by the startup code or the menu task,
  1293. // consoleplayer, displayplayer, playeringame[] should be set. 
  1294. //
  1295. skill_t d_skill; 
  1296. int     d_episode; 
  1297. int     d_map; 
  1298.  
  1299. void
  1300. G_DeferedInitNew
  1301. ( skill_t       skill,
  1302.   int           episode,
  1303.   int           map) 
  1304.     d_skill = skill; 
  1305.     d_episode = episode; 
  1306.     d_map = map; 
  1307.     gameaction = ga_newgame; 
  1308.  
  1309.  
  1310. void G_DoNewGame (void) 
  1311. {
  1312.     demoplayback = false; 
  1313.     netdemo = false;
  1314.     netgame = false;
  1315.     deathmatch = false;
  1316.     playeringame[1] = playeringame[2] = playeringame[3] = 0;
  1317.     respawnparm = false;
  1318.     fastparm = false;
  1319.     nomonsters = false;
  1320.     consoleplayer = 0;
  1321.     G_InitNew (d_skill, d_episode, d_map); 
  1322.     gameaction = ga_nothing; 
  1323.  
  1324. // The sky texture to be used instead of the F_SKY1 dummy.
  1325. extern  int     skytexture; 
  1326.  
  1327.  
  1328. void
  1329. G_InitNew
  1330. ( skill_t       skill,
  1331.   int           episode,
  1332.   int           map ) 
  1333.     int             i; 
  1334.          
  1335.     if (paused) 
  1336.     { 
  1337.         paused = false; 
  1338.         S_ResumeSound (); 
  1339.     } 
  1340.         
  1341.  
  1342.     if (skill > sk_nightmare) 
  1343.         skill = sk_nightmare;
  1344.  
  1345.  
  1346.     // This was quite messy with SPECIAL and commented parts.
  1347.     // Supposedly hacks to make the latest edition work.
  1348.     // It might not work properly.
  1349.     if (episode < 1)
  1350.       episode = 1; 
  1351.  
  1352.     if ( gamemode == retail )
  1353.     {
  1354.       if (episode > 4)
  1355.         episode = 4;
  1356.     }
  1357.     else if ( gamemode == shareware )
  1358.     {
  1359.       if (episode > 1) 
  1360.            episode = 1; // only start episode 1 on shareware
  1361.     }  
  1362.     else
  1363.     {
  1364.       if (episode > 3)
  1365.         episode = 3;
  1366.     }
  1367.     
  1368.  
  1369.   
  1370.     if (map < 1) 
  1371.         map = 1;
  1372.     
  1373.     if ( (map > 9)
  1374.          && ( gamemode != commercial) )
  1375.       map = 9; 
  1376.                  
  1377.     M_ClearRandom (); 
  1378.          
  1379.     if (skill == sk_nightmare || respawnparm )
  1380.         respawnmonsters = true;
  1381.     else
  1382.         respawnmonsters = false;
  1383.                 
  1384.     if (fastparm || (skill == sk_nightmare && gameskill != sk_nightmare) )
  1385.     { 
  1386.         for (i=S_SARG_RUN1 ; i<=S_SARG_PAIN2 ; i++) 
  1387.             states[i].tics >>= 1; 
  1388.         mobjinfo[MT_BRUISERSHOT].speed = 20*FRACUNIT; 
  1389.         mobjinfo[MT_HEADSHOT].speed = 20*FRACUNIT; 
  1390.         mobjinfo[MT_TROOPSHOT].speed = 20*FRACUNIT; 
  1391.     } 
  1392.     else if (skill != sk_nightmare && gameskill == sk_nightmare) 
  1393.     { 
  1394.         for (i=S_SARG_RUN1 ; i<=S_SARG_PAIN2 ; i++) 
  1395.             states[i].tics <<= 1; 
  1396.         mobjinfo[MT_BRUISERSHOT].speed = 15*FRACUNIT; 
  1397.         mobjinfo[MT_HEADSHOT].speed = 10*FRACUNIT; 
  1398.         mobjinfo[MT_TROOPSHOT].speed = 10*FRACUNIT; 
  1399.     } 
  1400.          
  1401.                          
  1402.     // force players to be initialized upon first level load         
  1403.     for (i=0 ; i<MAXPLAYERS ; i++) 
  1404.         players[i].playerstate = PST_REBORN; 
  1405.  
  1406.     usergame = true;                // will be set false if a demo 
  1407.     paused = false; 
  1408.     demoplayback = false; 
  1409.     automapactive = false; 
  1410.     viewactive = true; 
  1411.     gameepisode = episode; 
  1412.     gamemap = map; 
  1413.     gameskill = skill; 
  1414.  
  1415.     viewactive = true;
  1416.     
  1417.     // set the sky map for the episode
  1418.     if ( gamemode == commercial)
  1419.     {
  1420.         skytexture = R_TextureNumForName ("SKY3");
  1421.         if (gamemap < 12)
  1422.             skytexture = R_TextureNumForName ("SKY1");
  1423.         else
  1424.             if (gamemap < 21)
  1425.                 skytexture = R_TextureNumForName ("SKY2");
  1426.     }
  1427.     else
  1428.         switch (episode) 
  1429.         { 
  1430.           case 1: 
  1431.             skytexture = R_TextureNumForName ("SKY1"); 
  1432.             break; 
  1433.           case 2: 
  1434.             skytexture = R_TextureNumForName ("SKY2"); 
  1435.             break; 
  1436.           case 3: 
  1437.             skytexture = R_TextureNumForName ("SKY3"); 
  1438.             break; 
  1439.           case 4:       // Special Edition sky
  1440.             skytexture = R_TextureNumForName ("SKY4");
  1441.             break;
  1442.         } 
  1443.  
  1444.     G_DoLoadLevel (); 
  1445.  
  1446.  
  1447. //
  1448. // DEMO RECORDING 
  1449. // 
  1450. #define DEMOMARKER              0x80
  1451.  
  1452.  
  1453. void G_ReadDemoTiccmd (ticcmd_t* cmd) 
  1454.     if (*demo_p == DEMOMARKER) 
  1455.     {
  1456.         // end of demo data stream 
  1457.         G_CheckDemoStatus (); 
  1458.         return; 
  1459.     } 
  1460.     cmd->forwardmove = ((signed char)*demo_p++); 
  1461.     cmd->sidemove = ((signed char)*demo_p++); 
  1462.     cmd->angleturn = ((unsigned char)*demo_p++)<<8; 
  1463.     cmd->buttons = (unsigned char)*demo_p++; 
  1464.  
  1465.  
  1466. void G_WriteDemoTiccmd (ticcmd_t* cmd) 
  1467.     if (gamekeydown['q'])           // press q to end demo recording 
  1468.         G_CheckDemoStatus (); 
  1469.     *demo_p++ = cmd->forwardmove; 
  1470.     *demo_p++ = cmd->sidemove; 
  1471.     *demo_p++ = (cmd->angleturn+128)>>8; 
  1472.     *demo_p++ = cmd->buttons; 
  1473.     demo_p -= 4; 
  1474.     if (demo_p > demoend - 16)
  1475.     {
  1476.         // no more space 
  1477.         G_CheckDemoStatus (); 
  1478.         return; 
  1479.     } 
  1480.         
  1481.     G_ReadDemoTiccmd (cmd);         // make SURE it is exactly the same 
  1482.  
  1483.  
  1484.  
  1485. //
  1486. // G_RecordDemo 
  1487. // 
  1488. void G_RecordDemo (char* name) 
  1489.     int             i; 
  1490.     int                         maxsize;
  1491.         
  1492.     usergame = false; 
  1493.     strcpy (demoname, name); 
  1494.     strcat (demoname, ".lmp"); 
  1495.     maxsize = 0x20000;
  1496.     i = M_CheckParm ("-maxdemo");
  1497.     if (i && i<myargc-1)
  1498.         maxsize = atoi(myargv[i+1])*1024;
  1499.     demobuffer = Z_Malloc (maxsize,PU_STATIC,NULL); 
  1500.     demoend = demobuffer + maxsize;
  1501.         
  1502.     demorecording = true; 
  1503.  
  1504.  
  1505. void G_BeginRecording (void) 
  1506.     int             i; 
  1507.                 
  1508.     demo_p = demobuffer;
  1509.         
  1510.     *demo_p++ = VERSION;
  1511.     *demo_p++ = gameskill; 
  1512.     *demo_p++ = gameepisode; 
  1513.     *demo_p++ = gamemap; 
  1514.     *demo_p++ = deathmatch; 
  1515.     *demo_p++ = respawnparm;
  1516.     *demo_p++ = fastparm;
  1517.     *demo_p++ = nomonsters;
  1518.     *demo_p++ = consoleplayer;
  1519.          
  1520.     for (i=0 ; i<MAXPLAYERS ; i++) 
  1521.         *demo_p++ = playeringame[i];             
  1522.  
  1523.  
  1524. //
  1525. // G_PlayDemo 
  1526. //
  1527.  
  1528. char*   defdemoname; 
  1529.  
  1530. void G_DeferedPlayDemo (char* name) 
  1531.     defdemoname = name; 
  1532.     gameaction = ga_playdemo; 
  1533.  
  1534. void G_DoPlayDemo (void) 
  1535.     skill_t skill; 
  1536.     int             i, episode, map; 
  1537.          
  1538.     gameaction = ga_nothing; 
  1539.     demobuffer = demo_p = W_CacheLumpName (defdemoname, PU_STATIC); 
  1540.     if ( *demo_p++ != VERSION && !M_CheckParm("-forcedemo"))
  1541.     {
  1542.       fprintf( stderr, "Demo is from a different game version!\n");
  1543.             fflush(stderr);
  1544.       gameaction = ga_nothing;
  1545.       return;
  1546.     }
  1547.     
  1548.     skill = *demo_p++; 
  1549.     episode = *demo_p++; 
  1550.     map = *demo_p++; 
  1551.     deathmatch = *demo_p++;
  1552.     respawnparm = *demo_p++;
  1553.     fastparm = *demo_p++;
  1554.     nomonsters = *demo_p++;
  1555.     consoleplayer = *demo_p++;
  1556.         
  1557.     for (i=0 ; i<MAXPLAYERS ; i++) 
  1558.         playeringame[i] = *demo_p++; 
  1559.     if (playeringame[1]) 
  1560.     { 
  1561.         netgame = true; 
  1562.         netdemo = true; 
  1563.     }
  1564.  
  1565.     // don't spend a lot of time in loadlevel 
  1566.     precache = false;
  1567.     G_InitNew (skill, episode, map); 
  1568.     precache = true; 
  1569.  
  1570.     usergame = false; 
  1571.     demoplayback = true; 
  1572.  
  1573. //
  1574. // G_TimeDemo 
  1575. //
  1576. void G_TimeDemo (char* name) 
  1577. {        
  1578.     nodrawers = M_CheckParm ("-nodraw"); 
  1579.     noblit = M_CheckParm ("-noblit"); 
  1580.     timingdemo = true; 
  1581.     singletics = true; 
  1582.  
  1583.     defdemoname = name; 
  1584.     gameaction = ga_playdemo; 
  1585.  
  1586.  
  1587. /* 
  1588. =================== 
  1589. = G_CheckDemoStatus 
  1590. = Called after a death or level completion to allow demos to be cleaned up 
  1591. = Returns true if a new demo loop action will take place 
  1592. =================== 
  1593. */ 
  1594.  
  1595. boolean G_CheckDemoStatus (void) 
  1596.     int             endtime; 
  1597.          
  1598.     if (timingdemo) 
  1599.     { 
  1600.         endtime = I_GetTime ();
  1601.         I_Error ("timed %i gametics in %i realtics (%5.2f fps)",gametic 
  1602.                  , endtime-starttime, ((float)gametic/(float)endtime-(float)starttime)*35); 
  1603.     } 
  1604.          
  1605.     if (demoplayback) 
  1606.     { 
  1607.         if (singledemo) 
  1608.             I_Quit (); 
  1609.                          
  1610.         Z_ChangeTag (demobuffer, PU_CACHE); 
  1611.         demoplayback = false; 
  1612.         netdemo = false;
  1613.         netgame = false;
  1614.         deathmatch = false;
  1615.         playeringame[1] = playeringame[2] = playeringame[3] = 0;
  1616.         respawnparm = false;
  1617.         fastparm = false;
  1618.         nomonsters = false;
  1619.         consoleplayer = 0;
  1620.         D_AdvanceDemo (); 
  1621.         return true; 
  1622.     } 
  1623.  
  1624.     if (demorecording) 
  1625.     { 
  1626.         *demo_p++ = DEMOMARKER; 
  1627.         M_WriteFile (demoname, demobuffer, demo_p - demobuffer); 
  1628.         Z_Free (demobuffer); 
  1629.         demorecording = false; 
  1630.         I_Error ("Demo %s recorded",demoname); 
  1631.     } 
  1632.          
  1633.     return false; 
  1634.  
  1635.  
  1636.  
  1637.